db = require '../model/db'

exports.index = (req, res) ->
    res.render 'index'

exports.loginGet = (req, res) ->
    res.render 'login'

exports.loginPost = (req, res) ->
    UserModel = db.model 'User'
    
    UserModel.find (err, doc) ->
        if err or doc.length is 0
            res.render 'login', {msg: "username or password not valud"}
        else
            user = doc[0]
            req.session.user = user
            res.redirect '/'
        
        return
    return

exports.logoutGet = (req, res) ->
    req.session.destroy()
    res.redirect '/login'